package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.persistence.model.TMonthbalance;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.service.ITMonthBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 历史月结记录控制器
 *
 * @author fengshuonan
 * @Date 2018-03-22 14:00:07
 */
@Controller
@RequestMapping("/monthbalance")
public class TMonthbalanceController extends BaseController {

    private String PREFIX = "/system/monthbalance/";

    @Autowired
    private ITMonthBalanceService monthbalanceService;

    /**
     * 跳转到历史月结记录首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "monthbalance.html";
    }

    /**
     * 跳转到添加历史月结记录
     */
    @RequestMapping("/monthbalance_add")
    public String monthbalanceAdd() {
        return PREFIX + "monthbalance_add.html";
    }

    /**
     * 跳转到修改历史月结记录
     */
    @RequestMapping("/monthbalance_update/{monthbalanceId}")
    public String monthbalanceUpdate(@PathVariable Integer monthbalanceId, Model model) {
      TMonthbalance monthbalance = monthbalanceService.selectById(monthbalanceId);

        Map m=monthbalanceService.selectMonthBalanceBill(monthbalanceId,ShiroKit.getUser().getDotId());
        model.addAttribute("monthbalanceId",monthbalanceId);
        model.addAttribute("bal_bill",m);
        return PREFIX + "monthbalance_edit.html";
    }

    /**
     * 获取历史月结记录列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Integer limit, Integer offset) {
        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String name=getPara("name");
        String beginTime=getPara("beginTime");
        String endTime=getPara("endTime");
//        String status=getPara("status");
//        if(StringUtils.isEmpty(status)){
//            status="0";
//        }
       Map param=new HashMap();
       param.put("dot_id", ShiroKit.getUser().getDotId());
        param.put("name",name);
        param.put("beginTime",beginTime);
        param.put("endTime",endTime);
//        param.put("status",status);
        List<Map<String, Object>> list = monthbalanceService.monthBalanceList(page, param);
        pageInfoBT.setData(list);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }
    /**
     * 获取历史月结记录列表
     */
    @RequestMapping(value = "/monthbalanceList")
    @ResponseBody
    public Object monthbalanceList() {
        Page<Map<String, Object>> page = new Page(0, 1000);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        List<Map<String, Object>> list = null;
        String monthbalanceId=getPara("monthbalanceId");
        Map m=new HashMap();
        m.put("monthbalanceId",monthbalanceId);
        m.put("dot_id",ShiroKit.getUser().getDotId());
        list=monthbalanceService.monthbalanceList(page,m);
        pageInfoBT.setData(list);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }
    /**
     * 新增历史月结记录
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TMonthbalance monthbalance) {
        monthbalanceService.insert(monthbalance);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除历史月结记录
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer monthbalanceId) {
        monthbalanceService.deleteById(monthbalanceId);
        return SUCCESS_TIP;
    }

    /**
     * 修改历史月结记录
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TMonthbalance monthbalance) {
        monthbalanceService.updateById(monthbalance);
        return super.SUCCESS_TIP;
    }

    /**
     * 历史月结记录详情
     */
    @RequestMapping(value = "/detail/{monthbalanceId}")
    @ResponseBody
    public Object detail(@PathVariable("monthbalanceId") Integer monthbalanceId) {
        return monthbalanceService.selectById(monthbalanceId);
    }
}
